package io.a.sql.smartsense;

import com.google.common.collect.*;

import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import java.util.Set;

public class Test {
    public static void main(String[] args) {
        // 创建Multimap, key为HashMap, value为ArrayList
        Multimap<String, Integer> arrayListMultimap = ArrayListMultimap.create();
        arrayListMultimap.put("张三", 90);
        arrayListMultimap.put("张三", 80);
        arrayListMultimap.put("张三", 100);
        arrayListMultimap.put("李四", 88);

        System.out.println("Multimap key为HashMap, value为ArrayList: " + arrayListMultimap);

        // 创建Multimap, key为HashMap, value为HashSet
        Multimap<String, Integer> hashMultimap = HashMultimap.create();
        hashMultimap.put("张三", 90);
        hashMultimap.put("张三", 80);
        hashMultimap.put("张三", 100);
        hashMultimap.put("李四", 88);

        System.out.println("\nMultimap key为HashMap, value为HashSet: " + hashMultimap);

        // 创建Multimap, key为LinkedHashMap, value为LinkedList
        Multimap<String, Integer> linkedListMultimap = LinkedListMultimap.create();
        linkedListMultimap.put("张三", 90);
        linkedListMultimap.put("张三", 80);
        linkedListMultimap.put("张三", 100);
        linkedListMultimap.put("李四", 88);

        System.out.println("\nMultimap key为LinkedHashMap, value为LinkedList: " + linkedListMultimap);

        // 创建Multimap, key为LinkedHashMap, value为LinkedHashMap
        Multimap<String, Integer> linkedHashMultimap = LinkedHashMultimap.create();
        linkedHashMultimap.put("张三", 90);
        linkedHashMultimap.put("张三", 80);
        linkedHashMultimap.put("张三", 100);
        linkedHashMultimap.put("李四", 88);

        System.out.println("\nMultimap key为LinkedHashMap, value为LinkedHashMap: " + linkedHashMultimap);

        // 创建Multimap, key为TreeMap, value为TreeSet
        Multimap<String, Integer> treeMultimap = TreeMultimap.create();
        treeMultimap.put("张三", 90);
        treeMultimap.put("张三", 80);
        treeMultimap.put("张三", 100);
        treeMultimap.put("李四", 88);

        System.out.println("\nMultimap key为TreeMap, value为TreeSet: " + treeMultimap);

        // 创建不可变Multimap, 无法新增、更新或删除, key为ImmutableMap, value为ImmutableList
        Multimap<String, Integer> immutableListMultimap = ImmutableListMultimap.<String, Integer>builder().put("张三", 90).put("张三", 80).put("张三", 100).put("李四", 88).build();

        System.out.println("\nMultimap key为ImmutableMap, value为ImmutableList: " + immutableListMultimap);

        // 创建不可变Multimap, 无法新增、更新或删除, key为ImmutableMap, value为ImmutableSet
        Multimap<String, Integer> immutableSetMultimap = ImmutableSetMultimap.<String, Integer>builder().put("张三", 90).put("张三", 80).put("张三", 100).put("李四", 88).build();

        System.out.println("\nMultimap key为ImmutableMap, value为ImmutableSet: " + immutableSetMultimap);

        // 获取值
        Collection<Integer> values = arrayListMultimap.get("张三");
        System.out.println("\nMultimap 获取值集合: " + values);

        // 获取不存在key的值, 返回的是空集合, 而不是null
        Collection<Integer> valuesByNotExistsKey = arrayListMultimap.get("王五");
        System.out.println("\nMultimap 获取不存在的Key值集合: " + valuesByNotExistsKey);

        // 获取值集合添加值
        // 返回的是多重映射中关联的值的视图集合, 并没有创建新对象, 还是之前的对象, 所以操作值集合会影响之前的Multimap
        values.add(60);
        System.out.println("\nMultimap 被影响: " + arrayListMultimap);

        // 获取大小
        System.out.println("\nMultimap 大小:" + arrayListMultimap.size());

        // 判断是否为空
        System.out.println("\nMultimap 是否为空: " + arrayListMultimap.isEmpty());

        // 包含key
        System.out.println("\nMultimap 包含key: " + arrayListMultimap.containsKey("张三"));

        // 包含value
        System.out.println("\nMultimap 包含value: " + arrayListMultimap.containsValue(60));

        // 包含key-value键值对
        System.out.println("\nMultimap 包含key-value对: " + arrayListMultimap.containsEntry("张三", 60));

        // 替换value
        arrayListMultimap.replaceValues("张三", Arrays.asList(10, 20, 30));
        System.out.println("\nMultimap 替换value: " + arrayListMultimap);

        // 根据key-value删除
        arrayListMultimap.remove("张三", 10);
        System.out.println("\nMultimap 根据key-value删除: " + arrayListMultimap);

        // 根据key删除
        Collection<Integer> removeAll = arrayListMultimap.removeAll("张三");
        System.out.println("\nMultimap 根据key删除: " + removeAll);

        // 获取key集合
        Set<String> keySet = arrayListMultimap.keySet();
        System.out.println("\nMultimap 获取key集合(HashSet): " + keySet);
        Multiset<String> keys = arrayListMultimap.keys();
        System.out.println("\nMultimap 获取key集合(MultiSet): " + keys);

        // 获取所有的key-value
        Collection<Map.Entry<String, Integer>> entries = arrayListMultimap.entries();
        System.out.println("\n遍历key-value开始--------------------------");
        entries.forEach(entry -> System.out.println(entry.getKey() + " : " + entry.getValue()));
        System.out.println("\n遍历key-value结束--------------------------");

        // 转换为Map<K, Collection<V>>
        Map<String, Collection<Integer>> collectionMap = arrayListMultimap.asMap();
        System.out.println("\nMultimap 转换为Map<K, Collection<V>>: " + collectionMap);
    }
}
